package maptool

import (
	"encoding/json"
	"fmt"
	"hft/global"
	"hft/tools/map_tool/response"
	"io/ioutil"
	"net/http"
	"net/url"

	"github.com/tidwall/gjson"
)

type Amap struct{}

func NewAmapMapApi(uri string, params *url.Values) ([]byte, error) {

	key := global.OC_CONFIG.Amap.Key
	host := global.OC_CONFIG.Amap.Url

	params.Add("key", key)
	fmt.Println(params)

	request, err := url.Parse(host + uri + "?" + params.Encode())

	if nil != err {
		fmt.Printf("host error: %v", err)
		return nil, err
	}

	resp, err1 := http.Get(request.String())
	fmt.Printf("url: %s\n", request.String())
	defer resp.Body.Close()
	if err1 != nil {
		fmt.Printf("request error: %v", err1)
		return nil, err
	}
	body, err2 := ioutil.ReadAll(resp.Body)
	if err2 != nil {
		fmt.Printf("response error: %v", err2)
	}

	return body, nil
}

// 输入提示
func (*Amap) TipsSearch(keywords string, city string, poiType string, cityLimit bool, location string) ([]byte, error) {

	params := &url.Values{
		"keywords": []string{keywords},
		"type":     []string{poiType},
		"city":     []string{city},
		"location": []string{location},
	}

	if cityLimit {
		params.Add("citylimit", "true")
	} else {
		params.Add("citylimit", "false")
	}
	res, err := NewAmapMapApi("/v3/assistant/inputtips", params)
	if err != nil {
		return nil, err
	}
	return res, nil
}

func (*Amap) GetCityByIp(ip string) (data *response.IpRes, err error) {

	var amapRes *response.IpRes

	params := &url.Values{
		"ip": []string{ip},
	}

	res, err := NewAmapMapApi("/v3/ip", params)

	json.Unmarshal(res, &amapRes)

	if err != nil {
		return nil, err
	}

	return amapRes, nil

}

// 关键字搜索
func (*Amap) SearchV2(keyword string, types string, region string, city_limit string, page_num string, page_size string) string {

	if page_num == "" {
		page_num = "1"
	}

	if page_size == "" {
		page_size = "25"
	}

	if region == "" {
		region = "全国"
	}

	if city_limit == "" {
		city_limit = "true"
	}

	var params = &url.Values{
		"keywords":   []string{keyword},
		"region":     []string{region},
		"city_limit": []string{city_limit},
		"page_num":   []string{page_num},
		"page_size":  []string{page_size},
	}

	if types != "" {
		params.Add("types", types)
	}

	data, _ := NewAmapMapApi("/v5/place/text?parameters", params)

	return string(data)

}

// 周边搜索
func (*Amap) SearchAround(keywords string, types string, location string, radius string, region string, city_limit string, page_num string, page_size string) string {

	if radius == "" {
		radius = "5000"
	}

	if city_limit == "" {
		city_limit = "true"
	}

	if page_num == "" {
		page_num = "1"
	}

	if page_size == "" {
		page_size = "25"
	}

	if region == "" {
		region = "全国"
	}

	fmt.Println("关键词:", keywords, "类型:", types, "经纬度:", location, "半径:", radius, "区域:", region, "是否限制城市:", city_limit, page_num, page_size)

	var params = &url.Values{
		"location":   []string{location},
		"radius":     []string{radius},
		"region":     []string{region},
		"city_limit": []string{city_limit},
		"page_num":   []string{page_num},
		"page_size":  []string{page_size},
	}

	if types != "" {
		params.Add("types", types)
	}

	if keywords != "" {
		params.Add("keywords", keywords)
	}

	data, _ := NewAmapMapApi("/v5/place/around?parameters", params)

	return string(data)
}

// 输入提示
func (*Amap) InputTipsV2(keyword string, types string, location string, city string, citylimit string) string {

	if city == "" {
		city = "全国"
	}

	if citylimit == "" {
		citylimit = "true"
	}

	var params = &url.Values{
		"keywords":  []string{keyword},
		"city":      []string{city},
		"citylimit": []string{citylimit},
	}

	if types != "" {
		params.Add("type", types)
	}

	if location != "" {
		params.Add("location", location)
	}

	data, _ := NewAmapMapApi("/v3/assistant/inputtips?parameters", params)

	return string(data)
}

// 获取ip定位
func (*Amap) GetIp(ip string) string {
	data, _ := NewAmapMapApi("/v3/ip", &url.Values{"ip": []string{ip}})
	var res = string(data)
	var city = gjson.Get(res, "city").String()

	return city
}

var AmapApi = new(Amap)
